<template>
      <div class="Cart">
    <div class="top">
      <span><van-icon name="passed" />100%正品保证</span>
      <span><van-icon name="passed" />所有商品精挑细选</span>
      <span><van-icon name="passed" />售后无忧</span>
    </div>
    <div class="num">
      <div>
        <span>购物数量 </span>
        <span style="color: var(--cy-Color)"> {{ cartItem.length }}</span>
      </div>
      <button
        @click="
          select(isShow);
          isShow = !isShow;
        "
      >
        {{ checkedStatus }}
      </button>
      <!-- @click="on" -->
    </div>
    <div v-if="cartItem.length">
      <ul class="white">
        <li
          v-for="(item, index) in cartItem"
          :key="item.item.selectedSkuComb.id"
        >
          <van-checkbox
            :name="item.item.selectedSkuComb.id"
            label-disabled
            v-model="item.checked"
            @click="checkbox(item)"
          ></van-checkbox>
          <!-- <td>{{ index + 1 }}</td> -->
          <div class="flex">
            <router-link  v-if="item.item.selectedSkuComb.goods_id == undefined"
              tag="div"
              :to="`/detail/${item.item.selectedSkuComb.id}`"
              class="img" 
            >
              <img :src="item.item.selectedSkuComb.imgUrl" alt="图片" />
            </router-link>
            <router-link  v-else
              tag="div"
              :to="`/detail/${item.item.selectedSkuComb.goods_id}`"
              class="img"
            >
              <img :src="item.item.selectedSkuComb.imgUrl" alt="图片" />
            </router-link>
            <div class="f1 text">
              <!-- <h1 v-if="item.productInfo" class="van-ellipsis">
                    {{ item.productInfo.store_name }}
                  </h1> -->
              <!-- <h2 v-if="item.productInfo.attrInfo">
                    属性：{{ item.productInfo.attrInfo.suk }}
                  </h2> -->
              <h3>￥{{ item.item.selectedSkuComb.price / 100 }}</h3>
            </div>
            <div class="f1 btn">
              <button @click="handleReduce(item)" :disabled="item.count === 1">
                -
              </button>
              <span>
                {{ item.count }}
              </span>
              <button @click="handleAdd(item)">+</button>
            </div>
            <div class="f1" v-if="isShow">
              <button class="btnone" @click="handleRemove(index)">移除</button>
            </div>
          </div>
        </li>
        <!-- <van-button type="primary" @click="checkAll">全选</van-button> -->
        <div>
          <van-submit-bar
            :price="total"
            button-text="立即结算"
            tip-icon="info-o"
            @submit="onsumi"
          >
            <!-- disabled -->
            <div @click="checkAll">
              <van-checkbox v-model="$store.state.allChecked"
                >全选
              </van-checkbox>
            </div>
            <!-- <template #tip>
              你的收货地址不支持同城送,
              <span style="color: blue" @click="onClickEditAddress"
                >修改地址</span
              >
            </template> -->
          </van-submit-bar>
        </div>
      </ul>
    </div>
    <div v-else class="none">
      <van-empty
        class="custom-image"
        image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
        description="购物车为空，快去购物吧~"
      />
    </div>
    <!-- <div>{{LoginUser}}</div> -->
    <!-- </div> -->
  </div>
</template>
<script>
// import { mapMutations } from "vuex";
import { mapState } from "vuex";
import { Toast } from "vant";
export default {
  name: "Cart",
  props: {
    id: [String, Number],
    // price: Number,
    goods_id: [String, Number],
  },
  data() {
    return {
      // result: [],
      // resultAll: [],
      // checked: false,
      // allChecked: false,
      // cartlist: null,
      isShow: false,
      checkedStatus: "管理",
      // goods_id:null,
    };
  },
  created() {
    // this.$store.state.allChecked=false;
    console.log(this.$store.state.allChecked);
    console.log(this.cartItem);
    // this.cartItem=this.$store.state.cartItem.item
  },
  watch: {},
  computed: {
    cartItem() {
      return this.$store.state.cartItem;
    },
    total() {
      return this.$store.state.total;
    },
    addBuy(){
        return this.$store.state.addBuy;
    },
    ...mapState(["state"]),
  },
  methods: {
    checkbox(item) {
      this.$store.dispatch("SelectItem", item);
      // this.$store.state
      // this.$store.dispatch("AddbuyData",item)
    },
    checkAll() {
      this.$store.dispatch("AllSelect");
    },
    handleReduce(item) {
      this.$store.dispatch("SubData", item);
    },
    handleAdd(item) {
      this.$store.dispatch("AddCartData", item);
    },
    handleRemove(index) {
      this.$store.dispatch("DeleteCart", index);
    },
    // onClickEditAddress() {
    //   this.$router.push({ path: "/address" });
    // },
    select(isShow) {
      if (isShow == true) {
        this.checkedStatus = "管理";
      } else {
        this.checkedStatus = "取消";
      }
    },
    onsumi() {
      console.log(this.$store.state.addBuy);
      if (this.$store.state.total==0) {
        Toast("请选中商品再结算");
      }
      else{
        if(this.cartItem.checked==true){
          
          this.$store.state.addBuy=this.cartItem;
          
        }
        // this.$store.state.addBuy=this.cartItem;
        let index=this.cartItem.length
        this.$store.dispatch("DeleteCart", index);
          this.$router.push({ path: `/buy` });
      }
    },
  },
  mounted() {
    this.$store.state.total=0;
    this.$store.state.allChecked=false;
    this.$store.state.cartItem.map((ele) => {
      ele.checked = false;
      if (ele.checked) {
        this.$store.state.total += ele.item.selectedSkuComb.price * ele.count;
        this.$store.state.allChecked = true;
      }
      // this.$store.dispatch("DeleteCart",ele)
    });
  },
};
</script>
<style>
.van-checkbox__icon--checked .van-icon {
  background-color: var(--cy-Color);
  border-color: var(--cy-Color);
}
.van-checkbox--label-disabled {
  padding: 10px;
  width: 20px;
  height: 20px;
}
</style>
<style lang="less" scoped>
ul.white {
  li {
    margin: 10px 0px;
    background: white;
    /* height: 100px; */
    display: flex;
    padding: 5px;
    align-items: center;
  }
}
.Cart {
  background: #eee;
  height: 100%;
  font-size: 16px;
  margin-bottom: 6vh;
  .top {
    display: flex;
    background: #eee;
    color: #666;
    text-align: center;
    align-items: baseline;
    span {
      padding: 12px 8px;
      flex: 1;
      font-size: 0.3rem;
    }
  }
  .num {
    display: flex;
    padding: 10px;
    background: white;
    align-items: baseline;
    div {
      flex: 1;
    }
    button {
      background: white;
      border: 1px solid #000;
      padding: 3px 10px;
      border-radius: 3px;
    }
  }
}
.flex {
  position: relative;
  padding: 5px 0;
  display: flex;
  font-size: 16px;
  align-items: baseline;
  width: 100vw;
  button {
    height: 22px;
    text-align: center;
  }
  .img {
    width: 80px;
    height: 80px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 5px;
    }
  }
  .f1 {
    flex: 1;

    h1 {
      font-size: 14px;
      width: 170px;
    }
    h2 {
      color: #999;
      font-size: 12px;
    }
    h3 {
      font-size: 16px;
      color: var(--cy-Color);
      line-height: 60px;
    }
  }
  .text {
    position: absolute;
    top: 10px;
    left: 100px;
  }
  .btn {
    position: absolute;
    bottom: 10px;
    right: 10px;
    button {
      border: none;
      background: #eee;
      height: 20px;
      width: 20px;
    }
    span {
      background: #eee;
      height: 20px;
      font-size: 16px;
      padding: 1px;
      margin: 3px;
    }
  }
  .btnone {
    position: absolute;
    right: 10px;
    top: 5px;
    width: 50px;
    height: 25px;
    border-radius: 10px;
    border: none;
    background: var(--cy-Color);
    color: white;
  }
}
.none {
  height: 82vh;
  background: #eee;
}
.van-submit-bar {
  position: fixed;
  bottom: 50px;
}
.custom-image .van-empty__image {
  width: 90px;
  height: 90px;
}
</style>